#include <stdio.h>
#include <stdbool.h>

#define Size 10

// 初始化函数：对操作对象的初始状态赋值
void InitBoard(char (*Board)[Size])
{
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            Board[i][j] = '-';
        }
    }
}

// 打印棋盘
void PrintBoard(char (*Board)[Size])
{
    printf("    ");

    for (int i = 0; i < Size; i++)
    {
        printf("%4d", i + 1);
    }

    printf("\n");

    for (int i = 0; i < Size; i++)
    {
        printf("%4d", i + 1);
        for (int j = 0; j < Size; j++)
        {
            printf("%4c", Board[i][j]); // %4c 代表每个字符占四个字符的宽度
        }

        printf("\n");
    }
}

// 下棋的函数
bool MakeMove(char (*Board)[Size], int row, int column, char player)
{
    // 非法位置
    if (row < 0 || row >= Size || column < 0 || column >= Size)
    {
        printf("非法的下棋位置，请重新输入！\n");
        return false;
    }
    if (Board[row][column] != '-')
    {
        printf("该位置已经有棋子了，请重新选择位置！\n");
        return false;
    }
    Board[row][column] = player;
    return true;
}

//
bool IsValidPlace(char (*Board)[Size], int row, int column)
{
    if (row < 0 || row >= Size || column < 0 || column >= Size)
    {
        
        return false;
    }
    return true;
}

// 判断获胜
bool CheckIsWin(char (*Board)[Size], int row, int column, char player)
{
    int direction[4][2] =
        {
            {1, 0},
            {1, 1},
            {0, 1},
            {-1, 1}};

    

    // 遍历四个方向：数棋子数量
    for (int i = 0; i < 4; i++)
    {
        int count = 1;
        // 下一个棋子的位置
        int drow = row + direction[i][0];
        int dcolumn = column + direction[i][1];

        while (IsValidPlace(Board, drow, dcolumn) && Board[drow][dcolumn] == player)
        {
            count++;
            if (count >= 5)
            {
                return true;
            }
            drow = drow + direction[i][0];
            dcolumn = dcolumn + direction[i][1];
        }

        //反方向
        drow =  row - direction[i][0];
        dcolumn = column - direction[i][1];
        
        while (IsValidPlace(Board, drow, dcolumn) && Board[drow][dcolumn] == player)
        {
            count++;
            
            if (count >= 5)
            {
                return true;
            }
            drow = drow - direction[i][0];
            dcolumn = dcolumn - direction[i][1];
        }

    
    }
    return false;
}







// 向量法：判断坐标


bool CheckIsCount(char (*Board)[Size], int row, int column, char player,int number)
{
    int direction[4][2] =
        {
            {1, 0},
            {1, 1},
            {0, 1},
            {-1, 1}};

    // 遍历四个方向：数棋子数量
    for (int i = 0; i < 4; i++)
    {
        int count = 1;
        // 下一个棋子的位置
        int drow = row + direction[i][0];
        int dcolumn = column + direction[i][1];

        while (IsValidPlace(Board, drow, dcolumn) && Board[drow][dcolumn] == player)
        {
            count++;
            if (count >= number)
            {
                return true;
            }
            drow = drow + direction[i][0];
            dcolumn = dcolumn + direction[i][1];
        }

        //反方向
        drow = row - direction[i][0];
        dcolumn = column - direction[i][1];
        
        while (IsValidPlace(Board, drow, dcolumn) && Board[drow][dcolumn] == player)
        {
            count++;
            
            if (count >= number)
            {
                return true;
            }
            drow = drow - direction[i][0];
            dcolumn = dcolumn - direction[i][1];
        }

    
    }
    return false;
}



// 判断平局
bool CheckIsDraw(char (*Board)[Size])
{
    for (int i = 0; i < Size; i++)
    {
        for (int j = 0; j < Size; j++)
        {
            if (Board[i][j] == '-')
                ;
            return false;
        }
    }
    return true;
}




void AIMakeMove(char (*Board)[Size],int row,int column)
{ 
        //优先取胜策略
       for(int i=0;i<Size;i++)
        {
            for(int j=0;j<Size;j++)
            {
                if(Board[i][j] == '-')
                {
                    Board[i][j]='0';

                    if(CheckIsWin(Board,i,j,'0'))
                    {
                        
                        return ;
                    }
                    Board[i][j] = '-';
                }
            }
        }
       
       
       for(int k = 5  ; k >=2  ; k--)
       {
        for(int i=0;i<Size;i++)
        {
            for(int j=0;j<Size;j++)
            {
                if(Board[i][j] == '-')
                {
                    Board[i][j]='x';
                    if(CheckIsCount(Board,i,j,'x',k))
                    {
                        Board[i][j]='0';

                        return ;
                    }
                    Board[i][j] = '-';

                }
            }
        }
}


        //阻止玩家赢

        
        //阻止玩家四个


        //阻止玩家三个


        //阻止玩家两个


}

int main()
{
    char Board[Size][Size] = {0};

    InitBoard(Board);
    // PrintBoard(Board);

        char player = 'x'; 
        int row =0,column = 0;
        while(1)
        {
            PrintBoard(Board);
          
            printf("请|%c|选手下棋：",player);

            if(player == 'x')
            {
            //从终端读取输入
            //阻塞函数：会让程序进入等待状态，直到收到输入才会继续执行程序
              scanf("%d %d",&row,&column);
            
               if(MakeMove(Board,row-1,column-1,player)==false)
               {
                continue;
               }
            }
            else
            {
                printf("AI正在下棋。。。。。\n");
                AIMakeMove(Board,row-1,column-1);
            }
            
            
            if((CheckIsWin(Board,row-1,column-1,player)==true))
            {
                PrintBoard(Board);
                printf("恭喜|%c|选手获胜！\n",player);
                
                break;
            }
            if(CheckIsDraw(Board))
            {
                PrintBoard(Board);
                printf("平局！\n");
                break;
            }
            //换边
            player = (player == 'x')? '0':'x';

        }

    return 0;
}